package main.java.indi.zyj.c150.greedy;

public class _09_CanCompleteCircuit {

    public int canCompleteCircuit(int[] gas, int[] cost) {

        int i = 0;
        int len = gas.length;

        while (i < len){

            int gasNum = 0, costNum = 0, cnt = 0;

            while (cnt < len){
                int j = (cnt + i) % len;
                gasNum += gas[j];
                costNum += cost[j];
                if (costNum > gasNum) break;
                cnt++;
            }

            if (cnt == len){
                return i;
            }else {
                i = i + 1 + cnt;
            }

        }

        return -1;


    }


}
